Hello World
Step 0: Create a Boilerplate Spring Boot Project with Maven
- Maven directory structure
- Maven projects have
a pom.xml file
<groupId>element- the text in this xml element is set via Spring Initializr
<artifactId>element- the text in this xml element is set via Spring Initializr
<version>element- eg.
0.0.1-SNAPSHOTmeans it is not a1.0release version, but a WIP snapshot version
- eg.
<properties>element<java.version>element- the java version you picked in Spring Initializr
<dependencies>(plural) element- list of multiple
<dependency>(singular) elements, each at least with a<groupId>and<artifactId>elementspring-boot-starter-data-jdbc(to access databases)spring-boot-starter-web(web dependency with an embedded web server)h2(h2 database)spring-boot-starter-test(for testing)
- list of multiple
- if we need to add some more dependencies later, we will put them in the
pom.xml
mvnw (for Linux) and mvnw.cmd (for Windows)
- a Maven wrapper
- the Spring project comes with an embedded Maven version, so you do not need to download Maven
- with this you can run all the Maven commands you want
src
- (
srchas a standard Maven directory structure) mainjavacom.companyname.phth.photoz.clone(all java classes will live in this folder)PhotozCloneApplication(a java class that the Spring Initializr has put there)
resourcesapplication.properties(to configure the Spring Application)static(if we want to write static html pages)- "
staticfolder is used for serving web static content, all the css, js, html etc. (also default folder search by spring) docs.spring.io", stackoverflow
- "
templates(if we want to write dynamic html pages)- "
templatesfolder is a place where you put all the thymeleaf templates. It is a default directory (by default spring will look inside for any templates).", stackoverflow
- "
testjavacom.companyname.phth.photoz.clonePhotozCloneApplicationTests(a test class)
Step 1: Boot up the Boilerplate Spring Boot Project
- the
PhotozCloneApplicationclass has one method, themainmethod- the
main()method runs the Spring specific functionSpringApplication.run()and puts your classPhotozCloneApplication.classinside thatSpringApplication.run()method that it executes SpringApplication.run()boots up Spring Boot which means booting up the embedded web server and after that we should be able to see our page in the browser- this will log the following messages in the output window ("Run" window):
Starting service [Tomcat]which means your webserver is being booted upTomcat started on port 8080
- this will log the following messages in the output window ("Run" window):
- the
package com.companyname.phth.photoz.clone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PhotozCloneApplication {
public static void main(String[] args) {
SpringApplication.run(PhotozCloneApplication.class, args);
}
}
- this boilerplate Spring Boot project will only show an
error (type=Not Found, status=404)- because there is nothing to accept JSON messages or to send it back to the frontend ("REST API")
Step 2: Display a "Hello World" Message
- to get rid of the error message
error (type=Not Found, status=404)we build aHello Worldpage- for this we create a new class (via alt insert in the file explorer) called
PhotozControllerinside the base packagecom.companyname.phth.photoz.clone
- for this we create a new class (via alt insert in the file explorer) called
package com.companyname.phth.photoz.clone;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
public class PhotozController {
public String hello() {
return "Hello World!";
}
}
- problem: Spring does not understand what this
PhotozControllerclass is supposed to do, so we have to add the following annotations:
many things in Spring Boot work with annotations as a sort of marker to tell Spring that it has to do something special with that class
You just have to start typing @someAnnotation and then press Enter on one of the suggested annotations. IntelliJ will automatically import the right class.
package com.companyname.phth.photoz.clone;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PhotozController {
public String hello() {
return "Hello World!";
}
}
- now, when you start the Spring application Spring will look at all the classes inside the package (and also in all the subpackages) of the
PhotozCloneApplicationclass and it will look for any REST controllers there.- if Spring finds REST controllers it will create a new instance of
PhotozControllerfor the user, ie. for the application developer
- if Spring finds REST controllers it will create a new instance of
- problem: the methods/functions in REST controllers are special, Spring does not understand what these functions are supposed to do, so we need to tell Spring what to do with the
hello()method- more specifically, we need to map the
hello()method, "mapping" means- The user/the browser will send HTTP GET requests to our server. The path will be eg.
"/"(if we just want to alocalhost:8080/). - we want to respond to that GET request.
- The user/the browser will send HTTP GET requests to our server. The path will be eg.
- more specifically, we need to map the
- with the
@GetMapping("/")annotation, effectively, we are saying that the REST controllerPhotozControllerhas one methodhello()which should be executed by Spring whenever a request hits the endpoint"/"with the HTTP GET method
package com.companyname.phth.photoz.clone;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PhotozController {
@GetMapping("/")
public String hello() {
return "Hello World!";
}
}
You can also have the @PostMapping, @DeleteMapping, etc. if you have different HTTP methods involved.
by default, Spring does not automatically reload and register these new methods, so you have to stop and run the application again (or just "rerun" the application)
Summary: What we have just learned is that, if you only want to build the smallest possible "Hello World" page, it is enough to create one REST controller. When you boot up Spring, Spring is able to understand what you want. We want it to just execute the method and return the string "Hello World" whenever someone sends a GET request.